﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.DTO.JobGradeManagementDTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.HumanResources
{
    /// <summary>
    /// 职位等级services
    /// </summary>
    public class JobGradeManagementServices : ApplicationService, IPositionManagementServices
    {
        /// <summary>
        /// 职位管理仓储
        /// </summary>
        public readonly IRepository<JobGradeManagement, int> _jobGradeManagement;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="jobGradeManagement">职位管理仓储</param>
        public JobGradeManagementServices(IRepository<JobGradeManagement, int> jobGradeManagement)
        {
            _jobGradeManagement = jobGradeManagement;
        }

        /// <summary>
        /// 添加职位管理
        /// </summary>
        /// <param name="jobGradeManagementModel"></param>
        /// <returns></returns>
        public async Task<int> AddJobGradeManagementModel(JobGradeManagementModelDTO jobGradeManagementModel)
        {
            var jobGradeManagement = ObjectMapper.Map<JobGradeManagementModelDTO, JobGradeManagement>(jobGradeManagementModel);
            var jobGradeManagemen = await _jobGradeManagement.InsertAsync(jobGradeManagement);
            return jobGradeManagemen == null ? 0 : 1;
        }

        /// <summary>
        /// 显示职位信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<JobGradeManagementModelDTO>> QueryJobGradeManagement(QueryJobGradeManagementDTO queryJobGradeManagementDTO)
        {
            var jobGradeManagementModels = await _jobGradeManagement.GetListAsync();
            //条件查询职位等级编码
            jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeCoding), a => a.JobGradeCoding.Contains(queryJobGradeManagementDTO.JobGradeCoding)).ToList();
            //层次
            jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeLevel), a => a.JobGradeLevel.Contains(queryJobGradeManagementDTO.JobGradeLevel)).ToList();
            //名称
            jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeLevelName), a => a.JobGradeLevelName.Contains(queryJobGradeManagementDTO.JobGradeLevelName)).ToList();
            // 薪资范围下限
            jobGradeManagementModels = jobGradeManagementModels.WhereIf(queryJobGradeManagementDTO.SalaryRangelower > 0, a => a.SalaryRangelower >= queryJobGradeManagementDTO.SalaryRangelower).ToList();

            // 薪资范围上限
            jobGradeManagementModels = jobGradeManagementModels.WhereIf(queryJobGradeManagementDTO.SalaryRangelimits > 0, a => a.SalaryRangelimits <= queryJobGradeManagementDTO.SalaryRangelimits).ToList();
            var jobGradeManagementlist = ObjectMapper.Map<List<JobGradeManagement>, List<JobGradeManagementModelDTO>>(jobGradeManagementModels);
            return jobGradeManagementlist;

        }

        ///// <summary>
        ///// 显示职位信息
        ///// </summary>
        ///// <returns></returns>
        //public async Task<JsonResult> QueryJobGradeManagement(QueryJobGradeManagementDTO queryJobGradeManagementDTO)
        //{
        //    var jobGradeManagementModels = await _jobGradeManagement.GetListAsync();
        //    //条件查询职位等级编码
        //    jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeCoding), a => a.JobGradeCoding.Contains(queryJobGradeManagementDTO.JobGradeCoding)).ToList();
        //    //层次
        //    jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeLevel), a => a.JobGradeLevel.Contains(queryJobGradeManagementDTO.JobGradeLevel)).ToList();
        //    //名称
        //    jobGradeManagementModels = jobGradeManagementModels.WhereIf(!string.IsNullOrEmpty(queryJobGradeManagementDTO.JobGradeLevelName), a => a.JobGradeLevelName.Contains(queryJobGradeManagementDTO.JobGradeLevelName)).ToList();
        //    // 薪资范围下限
        //    jobGradeManagementModels = jobGradeManagementModels.WhereIf(queryJobGradeManagementDTO.SalaryRangelower > 0, a => a.SalaryRangelower >= queryJobGradeManagementDTO.SalaryRangelower).ToList();

        //    // 薪资范围上限
        //    jobGradeManagementModels = jobGradeManagementModels.WhereIf(queryJobGradeManagementDTO.SalaryRangelimits > 0, a => a.SalaryRangelimits <= queryJobGradeManagementDTO.SalaryRangelimits).ToList();
        //    jobGradeManagementModels= jobGradeManagementModels.OrderByDescending(a=>a.Id).Skip((queryJobGradeManagementDTO.PageIndex-1)* queryJobGradeManagementDTO.PageSize).Take(queryJobGradeManagementDTO.PageSize).ToList();

        //    var jobGradeManagementlist = ObjectMapper.Map<List<JobGradeManagement>, List<JobGradeManagementModelDTO>>(jobGradeManagementModels);
        //    return JsonArray;
        //}

        /// <summary>
        /// 回显单条职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<JobGradeManagementModelDTO> GetJobGradeManagementModel(int id)
        {
            var jobGradeManagement = await _jobGradeManagement.FindAsync(id);
            if (jobGradeManagement == null)
            {
                return null;
            }
            return ObjectMapper.Map<JobGradeManagement, JobGradeManagementModelDTO>(jobGradeManagement);
        }
        /// <summary>
        /// 修改职位信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> UpJobGradeManagement(JobGradeManagementModelDTO jobGradeManagementModelDTO) 
        {
            var jobGradeManagement = ObjectMapper.Map<JobGradeManagementModelDTO, JobGradeManagement>(jobGradeManagementModelDTO);
            var rolecount = await _jobGradeManagement.UpdateAsync(jobGradeManagement);
            return rolecount == null ? 0 : 1;
        }



        /// <summary>
        ///删除职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DelJobGradeManagement(int id)
        {

            await _jobGradeManagement.DeleteDirectAsync(a => a.Id == id);
        }

/// <summary>
/// 批量删除职位信息
/// </summary>
/// <param name="ids">要删除的职位ID列表</param>
/// <returns></returns>
public async Task BatchDelJobGradeManagement(List<int> ids)
{
    if (ids == null || !ids.Any())
        return;

    await _jobGradeManagement.DeleteDirectAsync(a => ids.Contains(a.Id));
}


    }
}
